const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')

let mainWindow
app.whenReady().then(() => {
  createWindow()
})

ipcMain.handle('handleClose', () => {
  mainWindow.close()
})
ipcMain.handle('handleMinimize', () => {
  mainWindow.minimize()
})
ipcMain.handle('handleMaximize', () => {
  if (mainWindow.isMaximized()) {
    mainWindow.unmaximize()
  } else {
    mainWindow.maximize()
  }
})

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 600,
    heigth: 500,
    resizable: false,
    frame: false,
    webPreferences: {
      nodeIntegration: false, // 开启node.js环境集成风险比较大
      sandbox: false, // 关闭沙箱不然preload中使用nodeapi报错
      preload: path.join(__dirname, '../preload/index2.js'), // 在prelaod中访问node大api
    }
  })
  mainWindow.loadFile(path.join(__dirname, '../renderer/index3.html'))
  mainWindow.webContents.openDevTools()
}